<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Personal_health_info extends  CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model("customer_health_model", "health_model");
		$this->load->model("personal_health_info_model", "health_info_model");
	}
	
	public function index()
	{
		$health_info = get_recent_health_info(get_uid());
		$health_info_fields = get_health_info_fields();
		
		$data = array(
				"health_info_fields"=>$health_info_fields,
				"health_info"=>$health_info
				);
		$this->load->view("functionEntry/personalHealthInfo/personalHealthInfo", $data);
	}
	

	
	private function is_field_shared($field)
	{
		$this->db->where("uid", get_uid());
		$query = $this->db->get("usershare");
		
		$result = $query->row_array();
		
		if(array_key_exists($field, $result))
			return $result[$field];
		
		return false;
	}
	
	
	
	public function update_health_data()
	{
		$field = $this->input->post("field");
		$data = $this->input->post("data");
		
		$this->health_model->update_health_data(get_uid(), $field, $data);
	}
	
	function get_history()
	{
		$this->load->model('personal_health_info_model');
		//json format example
		//$json = '{"PriceTicks":[{"Price":5.5,"TickDate":"\/Date(1283745600000)\/"},{"Price":6.8,"TickDate":"\/Date(1283832000000)\/"}]}';
		$key = $this->input->get('typeName');
	
		$json = '{"PriceTicks":[';
			
		$rows = $this->personal_health_info_model->get_history_by(get_uid(), $key);
			
		if($rows == null)
		{
			$this->get_history_unknown_attr();
			return;
		}
	
		foreach($rows as $row)
		{
			$json .= '{"Price":'.$row->$key.',"TickDate":"'.$row->date.'"},';
		}
		$json = substr($json, 0, strlen($json)-1);
		$json .= ']}';
	
		$msg = (array)json_decode($json, true);
		echo json_encode($msg);
	}
	
	function get_history_unknown_attr()
	{
		$datetime = mysql_datetime();
		$json = '{"PriceTicks":[{"Price":0,"TickDate":"'.$datetime.'"}]}';
		$msg = (array)json_decode($json, true);
		echo json_encode($msg);
	}
		
}
?>